package cn.edu.njust.epms.service.Impl;

import cn.edu.njust.epms.dao.ItemDao;
import cn.edu.njust.epms.entity.uItem;
import cn.edu.njust.epms.service.ItemService;
import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.common.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    ItemDao dao;
    @Override
    public Resp<uItem> apply(uItem i) {
        i.setoState(0);//state为0，表示待审核
        i.setoTime(new Date());
        boolean b = dao.add(i);
        if (b)  return Resp.success(i);
        else return Resp.error("申请提交失败！");
    }

    @Override
    public Resp<List<uItem>> selectByCondition(R r) {
        if(!(r.getName() == null||r.getName().equals("")||"".equals(r.getName())))
            r.setName("%"+r.getName()+"%");
        if(!(r.getPid() == null||r.getPid().equals("")||"".equals(r.getPid())))
            r.setPid("%"+r.getPid()+"%");
        List<uItem> uItems = dao.selectByCondition(r);
        return Resp.success(uItems);
    }

    @Override
    public Resp<uItem> audit(uItem i) {       //审批
        boolean update = dao.update(i);
        if (update) return  Resp.success(i);
        return Resp.error("更新失败！");
    }

    @Override
    public Resp<String> deleteOrder(Integer oId){
        boolean b = dao.deleteOrder(oId);
        if (b){
            return Resp.success("删除成功！");
        }else {
            return Resp.error("操作失败！");
        }
    }
}
